package com.api.shopapi.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.*;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 入住前的评估
 */
@RestController
@RequestMapping("ElderlyCheckinAssess")
public class ElderlyCheckinAssessController extends BaseController{

    @Autowired
    @Qualifier("ElderlyCheckinAssessServiceImplOne")
    private ElderlyCheckinAssessService elderlyCheckinAssessService;


    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("ElderlyServiceImplOne")
    private ElderlyService elderlyService;

    @Autowired
    @Qualifier("ElderlyCheckinAssessItemServiceImplOne")
    private ElderlyCheckinAssessItemService elderlyCheckinAssessItemService;

    @Autowired
    @Qualifier("CheckinAssessClassServiceImplOne")
    private CheckinAssessClassService checkinAssessClassService;


    @Autowired
    @Qualifier("CheckinAssessItemServiceImplOne")
    private CheckinAssessItemService checkinAssessItemService;


    @Autowired
    @Qualifier("CheckinAssessGroupServiceImplOne")
    private CheckinAssessGroupService checkinAssessGroupService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;


    @Autowired
    @Qualifier("InstitutionServiceImplOne")
    private InstitutionService institutionService;


    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;


    @Autowired
    @Qualifier("MedicinerServiceImplOne")
    private MedicinerService medicinerService;


    @Autowired
    @Qualifier("NurseServiceImplOne")
    private NurseService nurseService;


    @Autowired
    @Qualifier("ElderlyCheckinAssessWaitServiceImplOne")
    private ElderlyCheckinAssessWaitService elderlyCheckinAssessWaitService;


    //后台机构提交
    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String elderlyCode = jsonObject.getString("elderlyCode");
            JSONArray assessArr = jsonObject.getJSONArray("assessArr");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/ElderlyCheckinAssess/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            String elderlyName = elderlyService.getByElderlyNameToElderlyCode(elderlyCode);
            ElderlyCheckinAssess elderlyCheckinAssess = new ElderlyCheckinAssess();
            String elderlyCheckinAssessCode = UUID.randomUUID().toString();

            elderlyCheckinAssess.setElderlyCheckinAssessCode(elderlyCheckinAssessCode);
            elderlyCheckinAssess.setElderlyCode(elderlyCode);
            elderlyCheckinAssess.setElderlyName(elderlyName);
            elderlyCheckinAssess.setCreateTime(new Date());
            elderlyCheckinAssess.setStaffCode(staffCode);
            elderlyCheckinAssess.setInstitutionCode(institutionCode);
            elderlyCheckinAssess.setRecordType("admin");

            elderlyCheckinAssessService.insert(elderlyCheckinAssess,assessArr);



            Elderly elderly = elderlyService.getByElderlyCode(elderlyCode);

            //删除待评估
            List<ElderlyCheckinAssessWait> list = elderlyCheckinAssessWaitService.listByElderlyCode(elderlyCode);
            for(ElderlyCheckinAssessWait elderlyCheckinAssessWait:list){
                elderlyCheckinAssessWaitService.delete(elderlyCheckinAssessWait.getElderlyCheckinAssessWaitCode());
            }

            //新增新的待评估
            int spanDay = elderly.getAssessSpanDay();
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE,spanDay);

            ElderlyCheckinAssessWait elderlyCheckinAssessWait = new ElderlyCheckinAssessWait();
            elderlyCheckinAssessWait.setElderlyCheckinAssessWaitCode(UUID.randomUUID().toString());
            elderlyCheckinAssessWait.setElderlyCode(elderlyCode);
            elderlyCheckinAssessWait.setCreateTime(calendar.getTime());
            elderlyCheckinAssessWait.setInstitutionCode(elderly.getInstitutionCode());
            elderlyCheckinAssessWait.setGovernmentCode(elderly.getGovernmentCode());
            elderlyCheckinAssessWaitService.insert(elderlyCheckinAssessWait);


            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/ElderlyCheckinAssess/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //服务人员app提交
    @RequestMapping(value = "ServeAddApp", method = RequestMethod.POST)
    public MyResult ServeAddApp(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            String elderlyCode = jsonObject.getString("elderlyCode");
            JSONArray jsonArray = jsonObject.getJSONArray("jsonArray");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ElderlyCheckinAssess/ServeAddApp");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);
            String institutionCode = user.getInstitutionCode();

            Elderly elderly = elderlyService.getByElderlyCode(elderlyCode);

            String elderlyCheckinAssessCode = UUID.randomUUID().toString();

            ElderlyCheckinAssess elderlyCheckinAssess = new ElderlyCheckinAssess();
            elderlyCheckinAssess.setElderlyCheckinAssessCode(elderlyCheckinAssessCode);
            elderlyCheckinAssess.setElderlyCode(elderlyCode);
            elderlyCheckinAssess.setElderlyName(elderly.getName());
            elderlyCheckinAssess.setCreateTime(new Date());
            elderlyCheckinAssess.setInstitutionCode(institutionCode);

            if(user.getUserType() == 7){
                Mediciner mediciner = medicinerService.getByUserCode(userCode);

                elderlyCheckinAssess.setRecordType("mediciner");
                elderlyCheckinAssess.setMedicinerCode(mediciner.getMedicinerCode());
                elderlyCheckinAssess.setMedicinerName(user.getName());
            }
            if(user.getUserType() == 8){
                Nurse nurse = nurseService.getByUserCode(userCode);

                elderlyCheckinAssess.setRecordType("nurse");
                elderlyCheckinAssess.setNurseCode(nurse.getNurseCode());
                elderlyCheckinAssess.setNurseName(user.getName());
            }

            int all_fen = 0;

            for(int i=0;i<jsonArray.size();i++){

                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                String checkinAssessClassCode = jsonObject1.getString("checkinAssessClassCode");
                JSONArray items = jsonObject1.getJSONArray("items");

                CheckinAssessClass checkinAssessClass = checkinAssessClassService.getByCheckinAssessClassCode(checkinAssessClassCode);
                String checkinAssessClassName = checkinAssessClass.getName();

                int class_fen = 0;
                for (int k=0;k<items.size();k++) {

                    JSONObject jsonObject2 = items.getJSONObject(k);

                    String checkinAssessGroupCode = jsonObject2.getString("checkinAssessGroupCode");
                    String checkinAssessItemCode = jsonObject2.getString("checkinAssessItemCode");

                    int fraction = jsonObject2.getInteger("fraction");
                    class_fen += fraction;

                    CheckinAssessItem checkinAssessItem = checkinAssessItemService.getByCheckinAssessItemCode(checkinAssessItemCode);
                    CheckinAssessGroup checkinAssessGroup = checkinAssessGroupService.getByCheckinAssessGroupCode(checkinAssessGroupCode);

                    ElderlyCheckinAssessItem elderlyCheckinAssessItem = new ElderlyCheckinAssessItem();
                    elderlyCheckinAssessItem.setElderlyCheckinAssessItemCode(UUID.randomUUID().toString());
                    elderlyCheckinAssessItem.setElderlyCheckinAssessCode(elderlyCheckinAssessCode);
                    elderlyCheckinAssessItem.setCheckinAssessGroupCode(checkinAssessGroupCode);
                    elderlyCheckinAssessItem.setCheckinAssessGroupName(checkinAssessGroup.getName());
                    elderlyCheckinAssessItem.setCheckinAssessItemCode(checkinAssessItemCode);
                    elderlyCheckinAssessItem.setCheckinAssessItemName(checkinAssessItem.getName());
                    elderlyCheckinAssessItem.setFraction(checkinAssessItem.getFraction());
                    elderlyCheckinAssessItem.setCheckinAssessClassCode(checkinAssessClassCode);
                    elderlyCheckinAssessItem.setCheckinAssessClassName(checkinAssessClassName);
                    elderlyCheckinAssessItemService.insert(elderlyCheckinAssessItem);
                }

                all_fen += class_fen;

            }

            elderlyCheckinAssess.setFraction(all_fen);
            elderlyCheckinAssessService.insert2(elderlyCheckinAssess);


            //删除待评估
            List<ElderlyCheckinAssessWait> list = elderlyCheckinAssessWaitService.listByElderlyCode(elderlyCode);
            for(ElderlyCheckinAssessWait elderlyCheckinAssessWait:list){
                elderlyCheckinAssessWaitService.delete(elderlyCheckinAssessWait.getElderlyCheckinAssessWaitCode());
            }

            //新增新的待评估
            int spanDay = elderly.getAssessSpanDay();
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE,spanDay);

            ElderlyCheckinAssessWait elderlyCheckinAssessWait = new ElderlyCheckinAssessWait();
            elderlyCheckinAssessWait.setElderlyCheckinAssessWaitCode(UUID.randomUUID().toString());
            elderlyCheckinAssessWait.setElderlyCode(elderlyCode);
            elderlyCheckinAssessWait.setCreateTime(calendar.getTime());
            elderlyCheckinAssessWait.setInstitutionCode(elderly.getInstitutionCode());
            elderlyCheckinAssessWait.setGovernmentCode(elderly.getGovernmentCode());
            elderlyCheckinAssessWaitService.insert(elderlyCheckinAssessWait);


            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ElderlyCheckinAssess/ServeAddApp", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }




    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String elderlyName = jsonObject.getString("elderlyName");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/ElderlyCheckinAssess/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<ElderlyCheckinAssess> elderlyCheckinAssessPageInfo = elderlyCheckinAssessService.getPageList(pageIndex, pageSize, institutionCode,elderlyName);
            List<ElderlyCheckinAssessDTO> elderlyCheckinAssessDTOList = new ArrayList<>();
            for (ElderlyCheckinAssess elderlyCheckinAssess:elderlyCheckinAssessPageInfo.getList()){
                ElderlyCheckinAssessDTO elderlyCheckinAssessDTO = mapper.map(elderlyCheckinAssess,ElderlyCheckinAssessDTO.class);
                elderlyCheckinAssessDTOList.add(elderlyCheckinAssessDTO);
            }
            for (ElderlyCheckinAssessDTO elderlyCheckinAssessDTO:elderlyCheckinAssessDTOList){
                List<ElderlyCheckinAssessItem> elderlyCheckinAssessItemList = elderlyCheckinAssessItemService.getByElderlyCheckinAssessCode(elderlyCheckinAssessDTO.getElderlyCheckinAssessCode());
                elderlyCheckinAssessDTO.setElderlyCheckinAssessItemList(elderlyCheckinAssessItemList);
            }
            elderlyCheckinAssessPageInfo.setList(Collections.unmodifiableList(elderlyCheckinAssessDTOList));

            return success(elderlyCheckinAssessPageInfo);
        }catch (Exception e) {
            insertError("/ElderlyCheckinAssess/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //ins
    @RequestMapping(value = "GetPage", method = RequestMethod.POST)
    public MyResult GetPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String elderlyName = jsonObject.getString("elderlyName");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/ElderlyCheckinAssess/GetPage");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<ElderlyCheckinAssess> elderlyCheckinAssessPageInfo = elderlyCheckinAssessService.getPageList(pageIndex, pageSize, institutionCode,elderlyName);

            List<ElderlyCheckinAssessDTO> elderlyCheckinAssessDTOList = new ArrayList<>();
            for (ElderlyCheckinAssess elderlyCheckinAssess:elderlyCheckinAssessPageInfo.getList()){
                ElderlyCheckinAssessDTO elderlyCheckinAssessDTO = mapper.map(elderlyCheckinAssess,ElderlyCheckinAssessDTO.class);
                elderlyCheckinAssessDTOList.add(elderlyCheckinAssessDTO);
            }

            for (ElderlyCheckinAssessDTO elderlyCheckinAssessDTO:elderlyCheckinAssessDTOList){
                List<ElderlyCheckinAssessItem> elderlyCheckinAssessItemList = elderlyCheckinAssessItemService.getByElderlyCheckinAssessCodeGroupByCheckinAssessClassCode(elderlyCheckinAssessDTO.getElderlyCheckinAssessCode());
                List<CheckinAssessClassDTO1> checkinAssessClassDTO1List = new ArrayList<>();
                for (ElderlyCheckinAssessItem elderlyCheckinAssessItem:elderlyCheckinAssessItemList){
                    CheckinAssessClass checkinAssessClass = checkinAssessClassService.getByCheckinAssessClassCode(elderlyCheckinAssessItem.getCheckinAssessClassCode());
                    CheckinAssessClassDTO1 checkinAssessClassDTO1 = mapper.map(checkinAssessClass,CheckinAssessClassDTO1.class);
                    List<ElderlyCheckinAssessItem> elderlyCheckinAssessItemList1 = elderlyCheckinAssessItemService.getByElderlyCheckinAssessCodeAndCheckinAssessClassCode(elderlyCheckinAssessDTO.getElderlyCheckinAssessCode(),checkinAssessClass.getCheckinAssessClassCode());
                    //获取当个分类的得分
                    int sumScore = 0;
                    for (ElderlyCheckinAssessItem elderlyCheckinAssessItem1:elderlyCheckinAssessItemList1){
                        int fraction = elderlyCheckinAssessItem1.getFraction();
                        sumScore = sumScore+fraction;
                    }
                    checkinAssessClassDTO1.setElderlyCheckinAssessItemList(elderlyCheckinAssessItemList1);
                    checkinAssessClassDTO1.setSumScore(sumScore);
                    checkinAssessClassDTO1List.add(checkinAssessClassDTO1);
                }
                elderlyCheckinAssessDTO.setCheckinAssessClassDTO1List(checkinAssessClassDTO1List);
            }
            elderlyCheckinAssessPageInfo.setList(Collections.unmodifiableList(elderlyCheckinAssessDTOList));

            return success(elderlyCheckinAssessPageInfo);
        }catch (Exception e) {
            insertError("/ElderlyCheckinAssess/GetPage", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //ins app
    @RequestMapping(value = "InsPageApp", method = RequestMethod.POST)
    public MyResult InsPageApp(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            String elderlyName = jsonObject.getString("elderlyName");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ElderlyCheckinAssess/InsPageApp");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Institution institution = institutionService.getByUserCode(userCode);
            String institutionCode = institution.getInstitutionCode();

            PageInfo<ElderlyCheckinAssess> elderlyCheckinAssessPageInfo = elderlyCheckinAssessService.getPageList(pageIndex, pageSize, institutionCode,elderlyName);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",elderlyCheckinAssessPageInfo);

            return success(jsonObject1);
        }catch (Exception e) {
            insertError("/ElderlyCheckinAssess/InsPageApp", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //gov
    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String elderlyName = jsonObject.getString("elderlyName");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/ElderlyCheckinAssess/GovPage");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<ElderlyCheckinAssess> elderlyCheckinAssessPageInfo = elderlyCheckinAssessService.pageByGovCode(pageIndex, pageSize, staff.getGovernmentCode(), elderlyName);

            List<ElderlyCheckinAssessDTO> elderlyCheckinAssessDTOList = new ArrayList<>();

            for (ElderlyCheckinAssess elderlyCheckinAssess:elderlyCheckinAssessPageInfo.getList()){
                ElderlyCheckinAssessDTO elderlyCheckinAssessDTO = mapper.map(elderlyCheckinAssess,ElderlyCheckinAssessDTO.class);
                elderlyCheckinAssessDTOList.add(elderlyCheckinAssessDTO);
            }

            for (ElderlyCheckinAssessDTO elderlyCheckinAssessDTO:elderlyCheckinAssessDTOList){
                List<ElderlyCheckinAssessItem> elderlyCheckinAssessItemList = elderlyCheckinAssessItemService.getByElderlyCheckinAssessCodeGroupByCheckinAssessClassCode(elderlyCheckinAssessDTO.getElderlyCheckinAssessCode());
                List<CheckinAssessClassDTO1> checkinAssessClassDTO1List = new ArrayList<>();
                for (ElderlyCheckinAssessItem elderlyCheckinAssessItem:elderlyCheckinAssessItemList){
                    CheckinAssessClass checkinAssessClass = checkinAssessClassService.getByCheckinAssessClassCode(elderlyCheckinAssessItem.getCheckinAssessClassCode());
                    CheckinAssessClassDTO1 checkinAssessClassDTO1 = mapper.map(checkinAssessClass,CheckinAssessClassDTO1.class);
                    List<ElderlyCheckinAssessItem> elderlyCheckinAssessItemList1 = elderlyCheckinAssessItemService.getByElderlyCheckinAssessCodeAndCheckinAssessClassCode(elderlyCheckinAssessDTO.getElderlyCheckinAssessCode(),checkinAssessClass.getCheckinAssessClassCode());
                    //获取当个分类的得分
                    int sumScore = 0;
                    for (ElderlyCheckinAssessItem elderlyCheckinAssessItem1:elderlyCheckinAssessItemList1){
                        int fraction = elderlyCheckinAssessItem1.getFraction();
                        sumScore = sumScore+fraction;
                    }
                    checkinAssessClassDTO1.setElderlyCheckinAssessItemList(elderlyCheckinAssessItemList1);
                    checkinAssessClassDTO1.setSumScore(sumScore);
                    checkinAssessClassDTO1List.add(checkinAssessClassDTO1);
                }
                elderlyCheckinAssessDTO.setCheckinAssessClassDTO1List(checkinAssessClassDTO1List);
            }

            elderlyCheckinAssessPageInfo.setList(Collections.unmodifiableList(elderlyCheckinAssessDTOList));

            return success(elderlyCheckinAssessPageInfo);
        }catch (Exception e) {
            insertError("/ElderlyCheckinAssess/GovPage", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



    //患者，医生，护士，获取自己的入住评估的列表（分类）
    @RequestMapping(value = "ElderlyServePageApp", method = RequestMethod.POST)
    public MyResult ElderlyServePageApp(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ElderlyCheckinAssess/ElderlyServePageApp");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);

            PageInfo<ElderlyCheckinAssess> pageInfo = null;

            if(user.getUserType() ==1){
                Elderly elderly = elderlyService.getByUserCode(userCode);
                pageInfo = elderlyCheckinAssessService.pageByElderlyCode(pageIndex, pageSize, elderly.getElderlyCode());
            }
            if(user.getUserType() ==7){
                Mediciner mediciner = medicinerService.getByUserCode(userCode);
                pageInfo = elderlyCheckinAssessService.pageByMedCode(pageIndex, pageSize, mediciner.getMedicinerCode());
            }
            if(user.getUserType() ==8){
                Nurse nurse = nurseService.getByUserCode(userCode);
                pageInfo = elderlyCheckinAssessService.pageByNurseCode(pageIndex, pageSize, nurse.getNurseCode());
            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo);

            return success(jsonObject1);
        }catch (Exception e) {
            insertError("/ElderlyCheckinAssess/ElderlyServePageApp", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //根据评估列表（分类）获取分类的评估项
    @RequestMapping(value = "ElderlyServePageAppClass", method = RequestMethod.POST)
    public MyResult ElderlyServePageAppClass(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String elderlyCheckinAssessCode = jsonObject.getString("elderlyCheckinAssessCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ElderlyCheckinAssess/ElderlyServePageAppClass");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ElderlyCheckinAssess elderlyCheckinAssess = elderlyCheckinAssessService.getByElderlyCheckinAssessCode(elderlyCheckinAssessCode);

            List<CheckinAssessClass> list = checkinAssessClassService.getList(elderlyCheckinAssess.getInstitutionCode());

            List<CheckinAssessClassDTO> listDTO = new ArrayList<>();
            for(CheckinAssessClass checkinAssessClass:list){

                String checkinAssessClassCode = checkinAssessClass.getCheckinAssessClassCode();
                List<ElderlyCheckinAssessItem> list1 =
                        elderlyCheckinAssessItemService.getByElderlyCheckinAssessCodeAndCheckinAssessClassCode(elderlyCheckinAssessCode,checkinAssessClassCode);

                int sumScore = 0;
                for(ElderlyCheckinAssessItem elderlyCheckinAssessItem:list1){
                    sumScore += elderlyCheckinAssessItem.getFraction();
                }

                CheckinAssessClassDTO checkinAssessClassDTO = mapper.map(checkinAssessClass,CheckinAssessClassDTO.class);
                checkinAssessClassDTO.setListElderlyCheckinAssessItem(list1);

                checkinAssessClassDTO.setSumScore(sumScore);

                listDTO.add(checkinAssessClassDTO);

            }


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("list",listDTO);

            return success(jsonObject1);
        }catch (Exception e) {
            insertError("/ElderlyCheckinAssess/ElderlyServePageAppClass", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


}
